package com.abkj.platform.tail.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;

/**
 * 导出Excel
 * 
 * @author ServerZhang
 * @date 2016年6月2日
 */
public class ExportExcelUtil {

	/**
	 * 将List<Map>转换成生成HSSFWorkbook
	 * @param sheetName sheet 名称
	 * @param fieldsKey Map中对应的所有Key
	 * @param fieldsName 第一行的所有列名
	 * @param list
	 * @return
	 * @author ServerZhang
	 * @date 2016年6月2日
	 */
	public static HSSFWorkbook list2Bytes(String title, 
			String sheetName, String[] fieldsKey,
			String[] fieldsName, Integer [] width,
			List<Map<String, Object>> list) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(sheetName);
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 

		if (fieldsName.length-1 > 0) {
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fieldsName.length-1));//参数：起始行号，终止行号， 起始列号，终止列号
		}
		
		HSSFCell cell = row.createCell(0);
		cell.setCellValue(title);
		cell.setCellStyle(style);
		
		row = sheet.createRow(1);
		for (int i = 0, lenth = fieldsName.length; i < lenth; i++) {
			cell = row.createCell(i);
			cell.setCellValue(fieldsName[i]);
			cell.setCellStyle(style);
			sheet.setColumnWidth(i, width[i]);
		}

		for (int i = 0, length = list.size(); i < length; i++) {
			row = sheet.createRow((int) i + 2);
			Map<String, Object> map = list.get(i);
			for (int j = 0, length2 = fieldsKey.length; j < length2; j++) {
				Object object = map.get(fieldsKey[j]);
				if (object != null) {
					HSSFCell hssfCell = row.createCell(j);
					hssfCell.setCellValue(object.toString());
				}
			}
		}
		return wb;
	}
	
	/**
	 * 将List<Map>转换成生成HSSFWorkbook
	 * @param sheetName sheet 名称
	 * @param fieldsKey Map中对应的所有Key
	 * @param fieldsName 第一行的所有列名
	 * @param list
	 * @param region 需要合并的行列位置，注：Integer[] 长度必须为4
	 * @return
	 * @author ServerZhang
	 * @date 2016年6月7日
	 */
	public static HSSFWorkbook list2Bytes(String title, 
			String sheetName, String[] fieldsKey,
			String[] fieldsName, Integer [] width,
			List<Map<String, Object>> list,List<Integer[]> region) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(sheetName);
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
		
		if (fieldsName.length-1 > 0) {
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fieldsName.length-1));//参数：起始行号，终止行号， 起始列号，终止列号
		}
		
		if (region != null && region.size() > 0) {
			Iterator<Integer[]> iterator = region.iterator();
			while(iterator.hasNext()){
				Integer[] next = iterator.next();
				if (next.length == 4 && next[3] > next[2]) {
					sheet.addMergedRegion(new CellRangeAddress(next[0], next[1], next[2], next[3]));
				}
			}
		}
		
		HSSFCell cell = row.createCell(0);
		cell.setCellValue(title);
		cell.setCellStyle(style);
		
		row = sheet.createRow(1);
		for (int i = 0, lenth = fieldsName.length; i < lenth; i++) {
			cell = row.createCell(i);
			cell.setCellValue(fieldsName[i]);
			cell.setCellStyle(style);
			sheet.setColumnWidth(i, width[i]);
		}
		
		for (int i = 0, length = list.size(); i < length; i++) {
			row = sheet.createRow((int) i + 2);
			Map<String, Object> map = list.get(i);
			for (int j = 0, length2 = fieldsKey.length; j < length2; j++) {
				Object object = map.get(fieldsKey[j]);
				if (object != null) {
					HSSFCell hssfCell = row.createCell(j);
					hssfCell.setCellValue(object.toString());
				}
			}
		}
		return wb;
	}
}
